package com.zxmclasslibrarie.view.wtflist;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zxmclasslibrarie.view.activity.application.R;

public class MoreButton extends LinearLayout implements OnClickListener {

	private final int topM = 8;
	private final int bottomM = 10;

	public interface OnMBClickListener {
		public void moreButtonListener();
	}

	Context mContext = null;
	TextView mText = null;
	ProgressBar mProgressBar = null;

	private OnMBClickListener mOnMBClickListener = null;

	public MoreButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MoreButton(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		mContext = context;
		this.setBackgroundResource(R.color.black);// 单个元素背景
		createView();
		setViewDate();
		addChildView();
		setting();
	}

	private void createView() {
		mText = new TextView(mContext);
		mProgressBar = new ProgressBar(getContext(), null,
				android.R.attr.progressBarStyleSmall);
	}

	private void setViewDate() {
		mText.setText("正在加载");
	}

	private void addChildView() {
		this.addView(mProgressBar);
		this.addView(mText);
	}

	public void setMroeOnClick(OnMBClickListener linstener) {
		this.mOnMBClickListener = linstener;
		this.setOnClickListener(this);
	}

	private void setting() {

		LinearLayout.LayoutParams bp = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
		bp.gravity = Gravity.CENTER_HORIZONTAL;
		bp.topMargin = topM;
		bp.bottomMargin = bottomM;
		this.setLayoutParams(bp);
		this.setGravity(Gravity.CENTER);

		mText.setTextColor(Color.BLACK);
		LinearLayout.LayoutParams p = (LayoutParams) mProgressBar
				.getLayoutParams();
		p.rightMargin = 5;
		this.setClickable(false);
	}

	/**
	 * 按钮是否可以点击
	 * 
	 * @param b
	 */
	public void setButtonClick(boolean b) {
		this.setClickable(b);
		if (b) {
			mProgressBar.setVisibility(View.GONE);
			mText.setText("刷新更多");
		} else {
			mProgressBar.setVisibility(View.VISIBLE);
			mText.setText("正在加载");
		}

	}

	public void setEnd() {
		this.setClickable(false);
		mProgressBar.setVisibility(View.GONE);
		mText.setText("没有更多内容啦");
	}

	public void setMore() {
		this.setClickable(true);
		mProgressBar.setVisibility(View.GONE);
		mText.setText("刷新更多");
	}

	public void setLodding() {
		this.setClickable(false);
		mProgressBar.setVisibility(View.VISIBLE);
		mText.setText("正在加载");
	}

	@Override
	public void onClick(View v) {
		// setLodding();
		mOnMBClickListener.moreButtonListener();
	}

}
